﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LocalTempData
{
    List<MiniUserInfo> m_miniUserInfos;
    public List<MiniUserInfo> miniUserInfos
    {
        get
        {
            if (m_miniUserInfos == null)
                m_miniUserInfos = new List<MiniUserInfo>();

            return m_miniUserInfos;
        }
        set
        {
            m_miniUserInfos = value;
        }
    }

    public void AddMiniUserInfo(MiniUserInfo miniUserInfo)
    {
        if (miniUserInfos == null)
            miniUserInfos = new List<MiniUserInfo>();

        bool found = false;
        for(int i = 0; i < miniUserInfos.Count; i++)
        {
            if (miniUserInfos[i].userId == miniUserInfo.userId)
            {
                found = true;
                miniUserInfos[i] = miniUserInfo;
            }
        }
        if (!found)
            miniUserInfos.Add(miniUserInfo);
    }
    public void AddMiniUserInfo(MiniUserInfo[] _miniUserInfos)
    {
        if (miniUserInfos == null)
            miniUserInfos = new List<MiniUserInfo>();

        foreach (var item in _miniUserInfos)
        {
            AddMiniUserInfo(item);
        }
    }
}

public class LocalDataController 
{
    public Dictionary<long, MiniUserInfo> miniUserInfoDict = new Dictionary<long, MiniUserInfo>(); 
    public LocalDataController()
    {
        LoadLocalData();

        //GameDebug.Log($" miniUserInfos:{localTempData.miniUserInfos.GetListStr()}");

        foreach (var miniU in this.localTempData.miniUserInfos)
        {
            if(miniU != null)
                miniUserInfoDict[miniU.userId] = miniU;
        }
    }

    LocalTempData localTempData;
    private LocalTempData GetLocalData() => localTempData;

    public void GetMiniUserInfo(long[] uIds, Action<MiniUserInfo[]> callback)
    {
        List<long> toReq = new List<long>(uIds);

        foreach (var uId in uIds)
        {
            if (this.miniUserInfoDict.ContainsKey(uId))
            {
                toReq.Remove(uId);
            }
        }
        if (toReq.Count <= 0)
        {
            List<MiniUserInfo> ret = new List<MiniUserInfo>();
            foreach (var uId in uIds)
            {
                ret.Add(miniUserInfoDict[uId]);
            }
            callback?.Invoke(ret.ToArray());
            return;
        }

        RequestServerController.instance.RequestMiniUserInfo(toReq.ToArray(), (muis) =>
        {
            foreach (var mui in muis)
            {
                miniUserInfoDict[mui.userId] = mui;
            }
            this.localTempData.AddMiniUserInfo(muis);

            GameDebug.Log($"get userinfos:{localTempData.miniUserInfos.GetListStr()}");

            List<MiniUserInfo> ret = new List<MiniUserInfo>();
            foreach (var uId in uIds)
            {
                ret.Add(miniUserInfoDict[uId]);
            }
            callback?.Invoke(ret.ToArray());

            UpdateLocalData();
        });
    }

    public void LoadLocalData()
    {
        if (System.IO.File.Exists(savePath()))
        {
            string data = System.IO.File.ReadAllText(savePath());
            //GameDebug.Log($"read local data:{data}");
            localTempData = Newtonsoft.Json.JsonConvert.DeserializeObject<LocalTempData>(data);
        }
        else
        {
            localTempData = new LocalTempData();
            string dataStr = Newtonsoft.Json.JsonConvert.SerializeObject(localTempData);
            System.IO.File.WriteAllText(savePath(), dataStr);
        }
    }
    public void UpdateLocalData()
    {
        string str = Newtonsoft.Json.JsonConvert.SerializeObject(GetLocalData());

        System.IO.File.WriteAllText(savePath(), str);

        GameDebug.Log($"Update Local Temp Data:{str}");
    }

    public static string savePath()
    {
        //get
        {
            string folder = Application.persistentDataPath;
            if (!System.IO.Directory.Exists(folder))
            {
                System.IO.Directory.CreateDirectory(folder);
#if UNITY_IOS || UNITY_IPHONE
                GameDebug.Log("savePath,folder:" + folder);
                UnityEngine.iOS.Device.SetNoBackupFlag(folder);
#endif
            }
            return folder + $"/local_temp.data";
        }
    }

}
